package com.steven.ourcontest.chat.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.steven.ourcontest.R
import com.steven.ourcontest.base.BaseViewBinder
import com.steven.ourcontest.chat.bean.ChatReceiveMessage
import com.steven.ourcontest.chat.bean.ChatReceiveMessageBean
import com.steven.ourcontest.me.widget.GlideRoundTransform
import com.steven.ourcontest.net.NetConstants
import com.steven.ourcontest.utils.DateUtil

class MessageViewBinder(
    private val context: Context?,
    private val itemClickListener: ((uid: Long,name:String) -> Unit)
) : BaseViewBinder<ChatReceiveMessageBean, ViewHolder>() {
    override fun onCreateViewHolder(inflater: LayoutInflater, parent: ViewGroup): ViewHolder {
        val mInflater = inflater.inflate(R.layout.item_message_list, parent, false)
        return ViewHolder(mInflater)
    }

    override fun onBindViewHolder(itemHolder: ViewHolder, itemData: ChatReceiveMessageBean) {
        itemHolder.apply {
            tv_name?.text = itemData.chatReceiveMessage.sendUserName
            tv_content?.text = "[${itemData.msgNum}条] ${itemData.chatReceiveMessage.postMessage}"
            itemView.setOnClickListener {
                itemClickListener.invoke(itemData.chatReceiveMessage.sendUser,itemData.chatReceiveMessage.sendUserName)
            }
            if (context != null) {
                if (iv_icon != null) {
                    Glide.with(context)
                        .load(NetConstants.ICON_BASE_URL + itemData.chatReceiveMessage.sendUserIconUrl)
                        .error(R.drawable.ic_avatar)
                        .apply(RequestOptions.bitmapTransform(
                            GlideRoundTransform(
                                context,
                                10
                            )
                        ))
                        .into(iv_icon)
                }
            }
        }
    }
}

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val iv_icon: ImageView? = itemView.findViewById(R.id.iv_icon)
    val tv_name: TextView? = itemView.findViewById(R.id.tv_name)
    val tv_content: TextView? = itemView.findViewById(R.id.tv_content)
    val view_end: View? = itemView.findViewById(R.id.view_end)
}